﻿

using System;
using System.Data;
using Model;
namespace Dal
{
	public class DbClassDal
	{
		public DbClassDal()
		{
		}
 		#region Insert
 		public static int Insert(DbClass dbClass)
		{	
			string sql = @"Insert Into DbClass (
					CName,
					CUpId,
					CPic,
					CNote,
					CKeywords,
					CDescription,
					CStatus,
					COrder,
					CDatetime,
					CType,
					CLevel,
					CAllClassIds,
					CBgImg
				) values('{0}',{1},'{2}','{3}','{4}','{5}',{6},{7},'{8}',{9},{10},'{11}','{12}')";
			sql = string.Format(sql,
					SqlHelper.String(dbClass.CName),
					dbClass.CUpId,
					SqlHelper.String(dbClass.CPic),
					dbClass.CNote,
					SqlHelper.String(dbClass.CKeywords),
					SqlHelper.String(dbClass.CDescription),
					dbClass.CStatus,
					dbClass.COrder,
					dbClass.CDatetime,
					dbClass.CType,
					dbClass.CLevel,
					SqlHelper.String(dbClass.CAllClassIds),
					SqlHelper.String(dbClass.CBgImg)
				);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
		
		#region Update
 		public static int Update(DbClass dbClass)
		{	
			string sql = @"Update DbClass set 

					CName= '{0}',
					CUpId= {1},
					CPic= '{2}',
					CNote= '{3}',
					CKeywords= '{4}',
					CDescription= '{5}',
					CStatus= {6},
					COrder= {7},
					CDatetime= '{8}',
					CType= {9},
					CLevel= {10},
					CAllClassIds= '{11}',
					CBgImg= '{12}'
				where

					CId= {13}";
			sql = string.Format(sql,
					SqlHelper.String(dbClass.CName),
					dbClass.CUpId,
					SqlHelper.String(dbClass.CPic),
					dbClass.CNote,
					SqlHelper.String(dbClass.CKeywords),
					SqlHelper.String(dbClass.CDescription),
					dbClass.CStatus,
					dbClass.COrder,
					dbClass.CDatetime,
					dbClass.CType,
					dbClass.CLevel,
					SqlHelper.String(dbClass.CAllClassIds),
					SqlHelper.String(dbClass.CBgImg),
					dbClass.CId);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 cId)
		{
		
			string sql = @"Delete from DbClass where 
					CId= {0}";
			sql = string.Format(sql,
					cId
);
			return SqlHelper.ExecSql(sql);
		} 
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from DbClass a "; 
			return SqlHelper.Select(sql);
		}
				
		public static DbClass Select(Int32 cId)
		{
			DbClass dbClass = null;
			string sql = @"select a.* from DbClass a  where
					a.CId= {0}
";
			sql = string.Format(sql,
					cId
);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				dbClass = new DbClass(ds.Tables[0].Rows[0]);					
			return dbClass;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from DbClass a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from dbClass a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from DbClass a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}
